<?php

namespace App\Http\Controllers\Api;

use Illuminate\Http\Request;

use App\Http\Requests;
use App\Http\Controllers\Controller;
use Illuminate\Support\Facades\DB;
use Qiniu\Pili\Mac;
use Qiniu\Pili\Client;

/**
 * 
 *Api接口文档
 *Author：chenwaywong@gmail.com
 * 
 */


class RestFullcontroller extends Controller
{
     // protected $accessKey = 'mmxNs0v9xVdhOInlTUOrY2C1IzcW68_SfvzCILwf';
     // protected $secretKey = 'Q48BJUOEMGSRJGDzsNZEAnMBI14dHw5IRRhrRhCp';
    //
	public function getliveurl(){

    	   
    	    // 
		$n=$_GET['id'];

		$accessKey = 'mmxNs0v9xVdhOInlTUOrY2C1IzcW68_SfvzCILwf';
		$secretKey = 'Q48BJUOEMGSRJGDzsNZEAnMBI14dHw5IRRhrRhCp';
		$bucket = 'bucketnjxd';

		//$ak = "7O7hf7Ld1RrC_fpZdFvU8aCgOPuhw2K4eapYOdII";
		// $sk = "6Rq7rMSUHHqOgo0DJjh15tHsGUBEH9QhWqqyj4ka";
		$hubName = "livesnjxd";

		//创建hub
		// echo "================Create hub\n";
		$mac = new Mac($accessKey, $secretKey);
		$client = new Client($mac);
		$domain="pili-publish.lives.njxdkj.cn";
		$streamKey="njxd";

		$hub = $client->hub($hubName);

		// pili.RTMPPublishURL(publishDomain, hub, streamKey, mac, expireAfterSeconds)
		// 
		// $url = Qiniu\Pili\RTMPPublishURL($domain, $hub, 'njxd', 3600, $accessKey, $secretKey);

		// $stream = $hub->RTMPPublishURL($domain,$hub,'njxd',$mac,100);

		// print_r( );

		// 初始化签权对象。
			// $auth = new Auth($accessKey, $secretKey);
			// 
			// 
			// - RTMPPublishURL()         #生成带授权凭证的 RTMP 推流地址
			// 
		/*
		- RTMPPlayURL()            #生成 RTMP 播放地址
		- HLSPlayURL()             #生成 HLS 播放地址
		- HDLPlayURL()             #生成 HDL(HTTP-FLV) 播放地址
		- SnapshotPlayURL()        #生成直播封面地址
		- hub.Create()             #创建流
		- hub.Stream()             #获得流
		- hub.List()               #查询流列表
		- hub.ListLive()           #查询直播列表
		- stream.Info()            #查询流信息
		- stream.Disable()         #禁播流（秒级禁推/禁播）
		- stream.Enable()          #启用流
		- stream.LiveStatus()      #查询直播实时信息
		- stream.Save()            #保存直播回放
		- stream.HistoryActivity() #查询直播历史记录

		*/


		$stream = $hub->stream($streamKey);
		$status = $stream->liveStatus();
 		//RTMP 推流地址
		// $url = $hub->RTMPPublishUrl1($domain, $hubName, $streamKey, 3600,$accessKey, $secretKey);
		// $play=$hub->HLSPlayURL1($domain, $hubName, $streamKey);
		$url=$this->playurl($hub,$domain, $hubName, $streamKey,$n);


		// echo $url, "\n";
    	// print_r($status);

		$user['info']=DB::table('user')->get();
		$user['uptoken'] = $hub->listLiveStreams($streamKey, 5, "")['keys'];
		$user['url']=$url;
		// $user['play']=$play;
		$user['n']=$n;


		return response()->json($user);
    	// return view("Welcome",['name'=>$auth->uploadToken($bucket)]);
	}


	function stream(){

        return null;

	}

	function hub(){


         return null;
	}
	function playurl($hub,$domain,$hubName,$streamKey,$action){

		switch ($action) {
			case 'hls':
				$url=$hub->HLSPlayURL("pili-live-hls.lives.njxdkj.cn", $hubName, $streamKey);
			break;
			case 'flv':
				$url=$hub->HDLPlayURL($domain,$hubName,$streamKey);
			break;
			case 'snapshot':
				$url=$hub->SnapshotPlayURL("pili-live-snapshot.lives.njxdkj.cn",$hubName,$streamKey);
			break;
			case 'rtmp':
				$url=$hub->RTMPPlayURL("pili-live-rtmp.lives.njxdkj.cn",$hubName,$streamKey);
			break;
			
			default:
				$url=$hub->HLSPlayURL("pili-live-hls.lives.njxdkj.cn", $hubName, $streamKey);
			break;
		}
	
          return $url;
	   

	}
}
